import { getGoodsCount, getGoodsinfo, getGoodsList, goodsEdit } from "../../request/api"
import Vue from 'vue'
import { successAlert } from "../../utils/alert";
const state =  {
    info:{
        isShow:false,
        title:'',
        isAdd:true,
    },
    form:{
        first_cateid:'',
        second_cateid:'',
        goodsname:'',
        price:'',
        market_price:'',
        img:'',
        description:'',
        specsid:'',
        specsattr:'',
        isnew:1,
        ishot:1,
        status:1,
    },
    second_cate:[],
    second_specs:[],
    imageUrl:"",
    editor:'',
    tableData:[],
    isPaging:false,
    size:2,
    page:1,
    total:0,
}

const mutations = {
    changeImageUrl(state,url){
        state.imageUrl = url
    },
    changeForm(state,form){
        state.form = form
    },
    changeEditor(state,editor){
        state.editor = editor
    },
    changeTableData(state,data){
        state.tableData=data
    },
    changeTotal(state,total){
        state.total=total
    },
    changePage(state,page){
        state.page=page
    },
    changeIsPaging(state,isPaging){
        state.isPaging=isPaging
    },
    changeCate(state,cate){
        state.second_cate = cate
    }, 
    changeSpecs(state,specs){
        state.second_specs = specs;
    },
}

const actions = {
    imgUrlActions(context,url){
        context.commit('changeImageUrl',url)
    },
    formActions(context,form){
        context.commit('changeForm',form)
    },
    editorActions(context,editor){
        context.commit('changeEditor',editor)
    },
    pageActions(context,page){
        context.commit('changePage',page)
    },
    isPagingActions(context,isPaging){
        context.commit('changeIsPaging',isPaging)
    },
    goodsListActions(context){
        let params = {}
        if(context.state.isPaging){
            params = {
                size:context.state.size,
                page:context.state.page,
            };
        }else{
            params = {}
        }
        getGoodsList(params).then(res=>{
            if(res.code === 200){
                if((res.list === null || res.list.length === 0)&& context.state.page > 1){
                    let page = context.state.page - 1;
                    context.commit('changePage',page);
                    context.dispatch('goodsListActions');
                    return;
                } 
                context.commit('changeTableData',res.list)
            }
        }) 
    },
    goodsCountActions(context){
        getGoodsCount().then(res=>{
            context.commit('changeTotal',res.list[0].total)
        })
    },
    goodsinfoActions(context,id){
        getGoodsinfo({id}).then(res=>{
            if(res.code===200){
                res.list.specsattr=res.list.specsattr.split(',');
                res.list.id=id
                context.commit('changeForm',res.list)
                context.commit('changeImageUrl',Vue.prototype.$pregImg+res.list.img)
            }
        })
    },
    cateAction(context,cate){
        context.commit('changeCate',cate)
    },
    specsAction(context,specs){
        context.commit('changeSpecs',specs)
    },
    goodsEditsAction(context,data){
        goodsEdit(data).then(res=>{
            if(res.code===200){
                successAlert(res.msg)
            }
        })
    }
}

const getters = {
    info(state){
        return state.info
    },
    form(state){
        return state.form
    },
    imageUrl(state){
        return state.imageUrl
    },
    tableData(state){
        return state.tableData
    },
    editor(state){
        return state.editor
    },
    size(state){
        return state.size
    },
    total(state){
        return state.total
    },
    isPaging(state){
        return state.isPaging
    },
    second_cate(state){
        return state.second_cate
    },
    second_specs(state){
        return state.second_specs
    },
}

export default{
    state,
    mutations,
    actions,
    getters,
    namespaced:true,
}